package com.zttek.photosShareProject.modules.service.share.impl;

import com.zttek.photosShareProject.common.json.ShareVO;
import com.zttek.photosShareProject.common.json.UserVO;
import com.zttek.photosShareProject.common.solr.ShareDoc;
import com.zttek.photosShareProject.common.solr.UserDoc;
import com.zttek.photosShareProject.modules.service.login.IUserService;
import com.zttek.photosShareProject.modules.service.share.ISearchService;
import com.zttek.photosShareProject.modules.service.share.IShareService;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Y
 * Date: 2017-08-16
 * Time: 16:17
 */

@Service
public class SearchService implements ISearchService{

    @Autowired
    private SolrClient userSolrClient;

    @Autowired
    private SolrClient shareSolrClient;

    @Autowired
    private IUserService userService;

    @Autowired
    private IShareService shareService;

    @Override
    public List<UserDoc> searchForUser(Integer start, Integer rows, String name) throws Exception {
        List<UserDoc> docs = new ArrayList<>();
        SolrQuery sq = setParam(start, rows, "username:" + name);
        sq.setFilterQueries("valid:1","isShare:1");
        QueryResponse response;
        try {
            response = userSolrClient.query(sq);
            SolrDocumentList results = response.getResults();
            for (SolrDocument result : results) {
                UserDoc doc = new UserDoc();
                doc.setId((Long) result.get("userId"));
                doc.setUsername((String) result.get("username"));
                doc.setAccount((String) result.get("account"));
                doc.setValid((Integer) result.get("valid"));
                doc.setIsShare((Integer) result.get("isShare"));

                docs.add(doc);
            }
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }

        return docs;
    }

    @Override
    public List<UserVO> userDoc2VO(List<UserDoc> docs, Long userId) throws Exception {
        List<UserVO> vos = new ArrayList<>();
        for (UserDoc doc : docs) {
            vos.add(userService.user2VO(userService.selectUserById(doc.getId()), userId, true));
        }
        return vos;
    }

    @Override
    public List<ShareDoc> searchForShare(Integer start, Integer rows, String description) throws Exception {
        List<ShareDoc> docs = new ArrayList<>();
        SolrQuery sq = setParam(start, rows, "description:" + description);
        sq.setFilterQueries("valid:1","status:1 OR status:9");
        QueryResponse response;
        try {
            response = shareSolrClient.query(sq);
            SolrDocumentList results = response.getResults();
            for (SolrDocument result : results) {
                ShareDoc doc = new ShareDoc();
                doc.setId((Long) result.get("shareId"));
                doc.setDescription((String) result.get("description"));
                doc.setValid((int) result.get("valid"));
                doc.setStatus((int) result.get("status"));

                docs.add(doc);
            }
        } catch (SolrServerException | IOException e) {
            e.printStackTrace();
        }

        return docs;
    }

    @Override
    public List<ShareVO> shareDoc2VO(List<ShareDoc> docs, Long userId) throws Exception {
        List<ShareVO> vos = new ArrayList<>();
        List<Long> praises = shareService.getUserPraiseList(userId);
        List<Long> hides = shareService.getUserHideList(userId);
        List<Long> collects = shareService.getUserCollectList(userId);
        for (ShareDoc doc : docs) {
            vos.add(shareService.share2VO(shareService.selectShare(doc.getId()), userId, false));
        }
        return vos;
    }

    private SolrQuery setParam(Integer start, Integer rows, String param){
        SolrQuery sq = new SolrQuery();
        sq.setQuery(param)
                .setStart((start - 1) * rows)
                .setRows(rows);
        return sq;
    }
}
